/* 890. 查找和替换模式 */
/**
 * @param {string[]} words
 * @param {string} pattern
 * @return {string[]}
 */
/* 构造双射 */
var findAndReplacePattern = function (words, pattern) {
	const result = []
	for (const word of words) {
		if (match(word, pattern) && match(pattern, word)) {
			result.push(word)
		}
	}
	return result
}
const match = (word, pattern) => {
	const map = new Map()
	for (let i = 0; i < word.length; i++) {
		const x = word[i],
			y = pattern[i]
		if (!map.has(x)) {
			map.set(x, y)
		} else if (map.get(x) !== y) {
			//word 中的同意字母必须映射到 pattern 中的同一之母上
			return false
		}
	}
	return true
}
const result = findAndReplacePattern(['abc', 'cba', 'xyx', 'yxx', 'yyx'], 'abc')
console.log('result :>>', result)
